chrome.storage.sync.set({shortcutKey: {
  funKey: ['shiftKey'],
  ordinaryKeyCode: 68,
  setStr: "SHIFT + D"
}})

// 是否符合启动条件
function isConformStart(type, curTabId) {
  chrome.action.setIcon({ path: `images/icons/${type ? 'active_icon' : 'icon'}.png` })
  if (type) {
    // 判断当前是否已经注入过资源的方案，注入js的时候加上监听器，每次注入前都请求一下监听器，有回应说明已经注入过了
    chrome.tabs.sendMessage(curTabId, { type: 'verifyInject' }, function (res) {
      if (res == undefined) {
        chrome.scripting.insertCSS({
          target: { tabId: curTabId },
          files: ['./injectResources/common/common.css']
        });
        chrome.scripting.executeScript({
          target: { tabId: curTabId },
          files: ['./injectResources/common/onMessage.js', './injectResources/common/common.js']
        }, function() {
          // 注入成功
        });
        
      }
    })
  }
}

async function checkStart() {
  let [curTab] = await chrome.tabs.query({ active: true, currentWindow: true });
  if (curTab == undefined) {
    isConformStart(false)
    return
  }
  // isConformStart(curTab.url.indexOf('https://www.baidu.com') != -1, curTab.id)
  isConformStart(true, curTab.id)
}

chrome.tabs.onActivated.addListener(checkStart)
chrome.tabs.onUpdated.addListener(async function (tabId, { status }, tabMsg) {
  if (status == 'loading') return;
  checkStart()
})
